﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zen4SyncDashboard.DAL;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace Zen4SyncDashboard.ViewModel
{
    public class CreateTestScenarioViewModel : BaseViewModel
    {
        #region - Bound Properties -

        public activity CurrentActivity { get; private set; }

        public testScenario NewTestScenario { get; private set; }

        public ObservableCollection<activityType> ActivityTypes { get; private set; }

        #endregion - Bound Properties -

        #region - Commands -

        public RelayCommand AddCurrentActivityCommand { get; private set; }

        public RelayCommand AddNewTestScenarioCommand { get; private set; }

        #endregion - Commands -

        public CreateTestScenarioViewModel()
        {
            InitializeBoundProperties();
            InitializeCommands();
        }

        #region - Initialization Methods -

        private void InitializeBoundProperties()
        {
            NewTestScenario = Zen4SyncModel.TestScenario.Create();
            CurrentActivity = Zen4SyncModel.Activity.Create();

            Task.Factory
                .StartNew(() =>
                {
                    base.SetWorkStatus("Fetching Activity Types...");
                    ActivityTypes = Zen4SyncModel.ActivityType.GetAll();
                    OnPropertyChanged("ActivityTypes");

                    base.SetWorkStatus(false);
                });            
        }

        private void InitializeCommands()
        {
            AddCurrentActivityCommand = new RelayCommand(
                param => AddCurrentActivity(),
                param => CanAddCurrentActivity());

            AddNewTestScenarioCommand = new RelayCommand(
                param => AddNewTestScenario(),
                param => CanAddNewTestScenario());
        }

        #endregion - Initialization Methods -

        private bool CanAddNewTestScenario()
        {
            return NewTestScenario.IsValid();
        }

        private void AddNewTestScenario()
        {
            Zen4SyncModel.TestScenario.Add(NewTestScenario);

            NewTestScenario = new testScenario();
            OnPropertyChanged("NewTestScenario");
        }

        private bool CanAddCurrentActivity()
        {
            return CurrentActivity.IsValid();
        }

        private void AddCurrentActivity()
        {
            NewTestScenario.activities.Add(CurrentActivity);

            CurrentActivity = Zen4SyncModel.Activity.Create();
            OnPropertyChanged("CurrentActivity");
        }
    }
}
